// pages/login/login.js
import {checkPhone} from "../../utils/util" //手机号格式验证
import Ajax from "../../utils/ajax" //ajax
const regeneratorRuntime = require("../../utils/runtime")	//async插件
const app = getApp()

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		userPhone: null,
		yzCode: null,
		curYzm:null,
		yzBtnTxt: "获取验证码",
		yzBtnIsCanTap: false,
		showRealBtn: false,
		curYear: new Date().getFullYear(),
		phoneFocus:false,
		yzCodeFocus:false,
		duration:null,	//验证码延时
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		this.ajax = new Ajax()
	},

	/* 手机号输入触发 */
	phoneInput(e) {
		if (checkPhone(e.detail.value)) {
			this.data.yzBtnIsCanTap = true
		} else {
			this.data.yzBtnIsCanTap = false
		}
		this.setData({
			userPhone: e.detail.value,
			yzBtnIsCanTap: this.data.yzBtnIsCanTap
		})
		//真假登陆按钮切换
		this.ifShowRealBtn()
	},

	/* 点击获取验证码 */
	async getYzCode() {
		wx.showLoading({title: 'Loading…'})		//loading
		
		let yzm = await this.ajax.post('car_phone_code/sendSMS',{phoneCode:this.data.userPhone})
		
		wx.hideLoading()  //关闭loading
		
		if(yzm && yzm.length==6){
			wx.showToast({
				title: '验证码发送成功',
				duration: 2000
			})
			
			this.setData({
				curYzm:yzm,
				yzBtnIsCanTap: false	//禁止发送
			})
			
			//延时
			this.setData({duration:300})
			this.yzBtnTimer = setInterval(()=>{
				this.data.duration--
				this.setData({
					duration:this.data.duration
				})
				if(this.data.duration<=0){
					this.setData({
						yzBtnTxt:'获取验证码',
						yzBtnIsCanTap:true		//允许发送
					})
					clearInterval(this.yzBtnTimer)
				}else{
					this.setData({
						yzBtnTxt:this.data.duration+'s后重新获取'
					})
				}
			},1000)
			
		}else{
			wx.showToast({
				title: '验证码获取失败',
				icon: 'none',
				duration: 2000
			})
		}
	},

	/* 验证码输入触发 */
	yzCodeInput(e) {
		this.setData({
			yzCode: e.detail.value,
		})
		//真假登陆按钮切换
		this.ifShowRealBtn()
	},
	
	/* 真假登陆按钮切换 */
	ifShowRealBtn(){
		if (this.data.userPhone &&
			this.data.yzCode &&
			this.data.curYzm &&
			checkPhone(this.data.userPhone) &&
			this.data.curYzm == this.data.yzCode)
		{
			this.setData({
				showRealBtn: true
			})
		}else{
			this.setData({
				showRealBtn: false
			})
		}
	},
	
	/* 真登陆按钮 */
	async getUserInfo(e) {
		if (e.detail.userInfo) {
			app.globalData.userInfo = e.detail.userInfo
			//将手机号及个人信息保存到数据库
			wx.showLoading({title: 'Loading…'})		//loading
			let saveUserInfo = await this.ajax.post('car_passenger/registerPassenger',{
				avatarUrl: app.globalData.userInfo.avatarUrl,
				nickName:app.globalData.userInfo.nickName,
				userPhone: this.data.userPhone,
				xiaoOpenId:app.globalData.openid
			})
			wx.hideLoading()  //关闭loading
			//保存数据库成功则跳转到首页
			if(saveUserInfo.data.userPhone && saveUserInfo.data.userPhone.length>5){
				//保存数据
				app.globalData.dbUserData = saveUserInfo.data
				//跳转
				wx.navigateBack({
					delta: 1
				})
			}
		} else { //未授权
			wx.showToast({
				title: '授权失败',
				icon: 'none',
				duration: 2000
			})
		}
	},

	/* 假登陆按钮 */
	unRealSureBtn() {
		if (!this.data.userPhone) {
			wx.showToast({
				title: '请输入手机号',
				icon: 'none',
				duration: 2000
			})
			//获得焦点
			this.setData({
				phoneFocus:true
			})
		} else if (!this.data.yzCode) {
			wx.showToast({
				title: '请输入验证码',
				icon: 'none',
				duration: 2000
			})
			//获得焦点
			this.setData({
				yzCodeFocus:true
			})
		} else if (!checkPhone(this.data.userPhone)) {
			wx.showToast({
				title: '手机号格式错误',
				icon: 'none',
				duration: 2000
			})
			//获得焦点
			this.setData({
				userPhone:null,
				phoneFocus:true
			})
		}else if(!this.data.curYzm || this.data.curYzm != this.data.yzCode){
			wx.showToast({
				title: '验证码错误',
				icon: 'none',
				duration: 2000
			})
			//获得焦点
			this.setData({
				yzCode:null,
				yzCodeFocus:true
			})
		}
	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		//验证码延时计算
		this.setData({
			duration:this.data.duration - parseInt((Date.now() - this.data.onHideTime) / 1000)
		})
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {
		//验证码延时计算 缓存页面关闭时间
		this.setData({
			onHideTime:Date.now()
		})
	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
